package com.blockmeta.bbs.businesslibrary.umeng.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.base.app.BaseApp;
import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.baselibrary.utils.LoadPicUtil;
import com.blockmeta.bbs.baselibrary.utils.loading.ProgressBarUtil;
import com.blockmeta.bbs.businesslibrary.R;
import com.blockmeta.bbs.businesslibrary.umeng.AnimatorUtil;
import com.blockmeta.bbs.businesslibrary.umeng.BaseShareDialog;
import com.blockmeta.bbs.businesslibrary.umeng.MEDIA_TYPE;
import com.blockmeta.bbs.businesslibrary.umeng.ScreenShotUtil;
import com.blockmeta.bbs.businesslibrary.umeng.ShareResTask;
import com.blockmeta.bbs.businesslibrary.umeng.ShareRouter;
import com.blockmeta.bbs.businesslibrary.umeng.SharedUtils;
import com.blockmeta.bbs.businesslibrary.util.CodeUtils;
import com.umeng.socialize.bean.SHARE_MEDIA;

import uk.co.senab.photoview.log.LoggerDefault;

/**
 * @author AirThor
 * @date 2020/3/25.
 */
public class BlockMessageShare extends BaseShareDialog {
    private View mShareView;

    public BlockMessageShare(Context context, ShareRouter shareRouter) {
        super(context, shareRouter);
    }

    @Override
    protected void initContent() {
        ImageView imCircle = find(R.id.iv_im_circle);
        ImageView wechat = find(R.id.iv_wechat);
        ImageView ciecle = find(R.id.iv_wechat_circle);
        ImageView pic = find(R.id.iv_long_pic);
        ImageView qq = find(R.id.iv_qq);
        TextView title = find(R.id.tv_block_message);
        ImageView ivSource = find(R.id.iv_source_icon);
        TextView tvSource = find(R.id.tv_source);
        ImageView weibo = find(R.id.iv_weibo);
        TextView dismiss = find(R.id.tv_dismiss);
        mShareView = find(R.id.scroll_share_image);
        TextView shareTime = find(R.id.tv_share_time);
        TextView checkMan = find(R.id.tv_sure);
        final ImageView fastImage = find(R.id.fast_image);
        final TextView content = find(R.id.tv_block_content);

        BlockMessageShareData item = mShareRouter.getBlockMessageShareData();

        shareTime.setText(FormatUtil.getTimetoHMS(Long.parseLong(item.mPostTime), "EEEE yyyy-MM-dd HH:mm"));

        View shareBoard = find(R.id.v_share_bg);
        AnimatorUtil.bottomToTopAnim(shareBoard,500,false,0f);

        AnimatorUtil.bottomToTopAnim(imCircle,1200,1f);
        AnimatorUtil.bottomToTopAnim(wechat,1100,1f);
        AnimatorUtil.bottomToTopAnim(ciecle,1000,1f);
        AnimatorUtil.bottomToTopAnim(weibo,900,1f);
        AnimatorUtil.bottomToTopAnim(qq,800,1f);
        AnimatorUtil.bottomToTopAnim(pic,700,1f);

        title.setText(item.mTitle);
        content.setText(item.mContent);
        if (!TextUtils.isEmpty(item.mImage)){
            fastImage.setVisibility(View.VISIBLE);
            LoadPicUtil.loadPicWithGlideRatio(item.mImage,fastImage);
        }

        ImageView qr = find(R.id.iv_qr);
        int wh = DimentionsUtil.dp2px(85);
        Bitmap bitmap = CodeUtils.createImage(sUrl, wh, wh, BitmapFactory.decodeResource(BaseApp.getApp().getResources(),R.drawable.qr_logo));
        qr.setImageBitmap(bitmap);

        if (item.mSourceImage != null && item.mSourceImage.startsWith("http")) {
            ivSource.setVisibility(View.VISIBLE);
            LoadPicUtil.load(item.mSourceImage,ivSource);
        } else {
            ivSource.setVisibility(View.GONE);
        }
        tvSource.setText(item.mSourceName);

        imCircle.setOnClickListener(this);
        wechat.setOnClickListener(this);
        ciecle.setOnClickListener(this);
        pic.setOnClickListener(this);
        qq.setOnClickListener(this);
        weibo.setOnClickListener(this);
        dismiss.setOnClickListener(this);
    }

    @Override
    protected int setContentViews() {
        return R.layout.view_block_message_share;
    }

    @Override
    protected void setAttributes() {
        setAttribute(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT, Gravity.CENTER);
    }

    @Override
    protected void startShare(final MEDIA_TYPE mediaType) {
        ProgressBarUtil.showPopProgress(mShareRouter.getContext());
        if (mediaType.mMedia == SHARE_MEDIA.WEIXIN||mediaType.mMedia == SHARE_MEDIA.WEIXIN_CIRCLE){
            //如果是往微信和朋友圈发 则以网页的形式发送
            SharedUtils.shareWeb((Activity) mShareRouter.getContext(), mediaType.mMedia, mShareRouter.getUrl(),
                    mShareRouter.getThumb(), mShareRouter.getTitle(), mShareRouter.getDesc(),mShareRouter.getUMShareListener());
            ProgressBarUtil.dismissPopProgress();
            safeDismiss();
        }else {
            ShareResTask.doTask(new ShareResTask.NormalBitmapTask(mShareView) {
                @Override
                public void doOnUIThread(Bitmap bitmap) {
                    share(bitmap, mediaType);
                    ProgressBarUtil.dismissPopProgress();
                    safeDismiss();
                }
            });
        }
    }

    private void share(Bitmap bitmap, MEDIA_TYPE mediaType) {
        ScreenShotUtil.zoomImg(bitmap, 1080);
        if (mediaType == MEDIA_TYPE.SAVE){
            ShareResTask.doTask(new ShareResTask.SaveImgTask(mShareRouter.getContext(),bitmap));
        }else if (mediaType == MEDIA_TYPE.IM){
            SharedUtils.shareIMImage((Activity) mShareRouter.getContext(),bitmap);
        }else {
            SharedUtils.sharePic((Activity) mShareRouter.getContext(),mediaType.mMedia,bitmap,mShareRouter.getUMShareListener());
        }
    }
}
